package org.real.guetshop.controller.admin;

import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.real.guetshop.propertis.JwtProperties;
import org.real.guetshop.constant.MessageConstant;
import org.real.guetshop.pojo.dto.EmployeeDTO;
import org.real.guetshop.pojo.dto.EmployeePageQueryDTO;
import org.real.guetshop.result.PageResult;
import org.real.guetshop.service.EmployeeService;
import org.real.guetshop.utils.JwtUtil;
import org.springframework.web.bind.annotation.*;
import org.real.guetshop.pojo.dto.EmployeeLoginDTO;
import org.real.guetshop.pojo.entity.Employee;
import org.real.guetshop.pojo.vo.EmployeeLoginVO;
import org.real.guetshop.result.Result;

import java.util.Date;

@Slf4j
@RestController
@RequestMapping("/admin/employee")
public class EmployeeController {

    @Resource
    private EmployeeService employeeService;

    @Resource
    private JwtProperties jwtProperties;


    /**
     * 员工登录
     * @param employeeLoginDTO
     * @return
     */
    @Operation(summary = "员工登录")
    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        //获得当前时间的秒数
        long nowTime = System.currentTimeMillis();
        //登录成功后下发jwt令牌

        //Data对象传入的数值是毫秒值

        String token = JwtUtil.createToken(employee.getId()
                , new Date(nowTime)
                , new Date(nowTime + jwtProperties.getAdminTtl()));

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(Long.valueOf(employee.getId()))
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }

    /**
     * 员工退出
     * @return
     */
    @Operation(summary = "员工退出")
    @PostMapping("/logout")
    public Result<String> logout() {return Result.success();}

    /**
     * 新增员工
     * @param employeeDTO
     * @return
     */
    @Operation(summary = "员工新增")
    @PostMapping("/add")
    public Result addNewEmployee(@RequestBody EmployeeDTO employeeDTO) {
        log.info("增加新的员工数据：{}", employeeDTO);
        employeeService.addNewEmployee(employeeDTO);
        return Result.success();
    }

    /**
     * 员工分页查询
     * @param employeePageQueryDTO
     * @return
     */
    @Operation(summary = "员工分页查询")
    @PostMapping("/page")
    public Result<PageResult> page(@RequestBody EmployeePageQueryDTO employeePageQueryDTO) {
        log.info("员工分页查询，参数为：{}", employeePageQueryDTO);
        PageResult pageResult = employeeService.pageQuery(employeePageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 删除员工
     * @param employeeDTO
     * @return
     */
    @Operation(summary = "员工删除")
    @DeleteMapping("/delete")
    public Result delete(@RequestBody EmployeeDTO employeeDTO) {
        log.info("开始删除员工：{}", employeeDTO);

        employeeService.deleteEmployee(employeeDTO);
        return Result.success();
    }

    /**
     * 修改员工信息
     * @param employeeDTO
     * @return
     */
    @PutMapping("/update")
    @Operation(summary = "员工信息修改")
    public Result update(@RequestBody EmployeeDTO employeeDTO) {
        log.info("开始修改员工信息：{}", employeeDTO);
        employeeService.updateEmployee(employeeDTO);
        return Result.success();
    }

    @Operation(summary = "根据id查询员工")
    @GetMapping("/{id}")
    private Result<Employee> getById(@PathVariable Integer id) {
        log.info("根据id查询员工：{}",id);
        Employee employee = employeeService.getById(id);
        if (employee == null){
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }
        return Result.success(employee);
    }
}
